package com.qf.servlet;

import com.qf.convert.MyConvert;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImp;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );

        Map<String, String[]> map = request.getParameterMap ( );
        User user = new User ();

        //使用common-beanutils.jar ----.有一个转换器工具类--->开发者可以自定义日期转换器
        ConvertUtils.register(new MyConvert (), Date.class); //注册转换器

        try {
            BeanUtils.populate ( user,map );
        } catch (IllegalAccessException e) {
            e.printStackTrace ( );
        } catch (InvocationTargetException e) {
            e.printStackTrace ( );
        }
        UserService userService = new UserServiceImp ();
        userService.updateUser (user);
        System.out.println (user );
        request.getRequestDispatcher("/findAllUser").forward(request,response);




    }

    protected void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {
        doPost ( request,response );
    }
}
